﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	internal class MethodFixedPipeDiameter
	{
		private static void GetListThree(out List<int> lst1, out List<int> lst2)
		{
			lst1 = new List<int>();
			lst2 = new List<int>();
			lst1.Add(65);
			lst1.Add(80);
			lst1.Add(100);
			lst1.Add(125);
			lst1.Add(150);
			lst1.Add(200);
			lst2.Add(40);
			lst2.Add(40);
			lst2.Add(65);
			lst2.Add(80);
			lst2.Add(100);
			lst2.Add(100);
		}

		private static void GetListFour(out List<int> lst1, out List<int> lst2)
		{
			lst1 = new List<int>();
			lst2 = new List<int>();
			lst1.Add(65);
			lst1.Add(80);
			lst1.Add(100);
			lst1.Add(125);
			lst1.Add(150);
			lst1.Add(200);
			lst1.Add(250);
			lst2.Add(32);
			lst2.Add(40);
			lst2.Add(50);
			lst2.Add(65);
			lst2.Add(80);
			lst2.Add(100);
			lst2.Add(100);
		}

		private static IList<Pipe> SortListPipe(IList<Pipe> pipes)
		{
			IList<Pipe> list = new List<Pipe>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipes[0]);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(pipes[1]);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(pipes[2]);
			if (3 == pipes.Count)
			{
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsXYParallel(hypiping2.Piping))
				{
					list.Add(hypiping3.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping2.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsXYParallel(hypiping3.Piping))
				{
					list.Add(hypiping2.Piping);
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = pipes;
				}
			}
			else if (4 == pipes.Count)
			{
				YJKPiping<Pipe> hypiping4 = new YJKPiping<Pipe>(pipes[3]);
                pipes[0].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
                pipes[1].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
                pipes[2].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
                pipes[3].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
				if (hypiping.IsPipingOnLine(hypiping2.Piping) || hypiping.IsXYParallel(hypiping2.Piping))
				{
					list = pipes;
				}
				else if (hypiping.IsPipingOnLine(hypiping3.Piping) || hypiping.IsXYParallel(hypiping3.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping3.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping4.Piping);
				}
				else if (hypiping.IsPipingOnLine(hypiping4.Piping) || hypiping.IsXYParallel(hypiping4.Piping))
				{
					list.Add(hypiping.Piping);
					list.Add(hypiping4.Piping);
					list.Add(hypiping2.Piping);
					list.Add(hypiping3.Piping);
				}
				else
				{
					list = pipes;
				}
			}
			return list;
		}

		public static int GetNewTeeFittingStatus(IList<Pipe> lstPipe)
		{
			if (lstPipe.Count != 3)
			{
				return -1;
			}
			int num = 0;
			IList<Pipe> list = MethodFixedPipeDiameter.SortListPipe(lstPipe);
            double num2 = list[0].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num3 = list[1].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num4 = list[2].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
			List<int> list2 = new List<int>();
			List<int> list3 = new List<int>();
			MethodFixedPipeDiameter.GetListThree(out list2, out list3);
			int num5 = 0;
			for (int i = 0; i < list2.Count; i++)
			{
				if ((double)list2[i] >= num3)
				{
					num5 = list3[i];
					break;
				}
			}
			if (num5 == 0)
			{
				num5 = list3.Last<int>();
			}
			if (num3 != num4)
			{
				return -1;
			}
			num++;
			if (num3 < 60.0 || num3 > 200.0)
			{
				return -1;
			}
			num++;
			if (num2 <= (double)num5 && num2 >= 25.0)
			{
				num++;
				if (num == 3)
				{
					if (num3 == 65.0 || num4 == 80.0)
					{
						num++;
					}
					if (num2 >= 25.0 && num2 <= 40.0)
					{
						num++;
					}
				}
				return num;
			}
			return -1;
		}

		public static bool BeSKTreeFitting(IList<Pipe> listPipe, bool beSKPipeType, double minValue, double maxValue)
		{
			bool result = false;
			if (listPipe == null || listPipe.Count != 3 || !beSKPipeType)
			{
				return result;
			}
			IList<Pipe> list = MethodFixedPipeDiameter.SortListPipe(listPipe);
            double num = list[0].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num2 = list[1].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num3 = list[2].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
			if (num >= num2 && num >= num3)
			{
				return result;
			}
			if (num <= 80.0 && ((num2 > minValue && num2 <= maxValue) || (num3 > minValue && num3 <= maxValue)))
			{
				result = true;
			}
			return result;
		}

		public static bool BeSKCrossFitting(IList<Pipe> listPipe, bool beSKPipeType, double minValue, double maxValue)
		{
			bool result = false;
			if (listPipe == null || listPipe.Count != 4 || !beSKPipeType)
			{
				return result;
			}
			IList<Pipe> list = MethodFixedPipeDiameter.SortListPipe(listPipe);
            double num = list[0].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num2 = list[1].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num3 = list[2].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num4 = list[3].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
			double num5 = (num > num2) ? num : num2;
			double num6 = (num3 > num4) ? num3 : num4;
			if (num5 <= 80.0 && num6 <= 80.0)
			{
				return result;
			}
			double num7 = (num5 < num6) ? num5 : num6;
			double num8 = (num5 > num6) ? num5 : num6;
			if (num7 <= 80.0 && num8 > minValue && num8 <= maxValue)
			{
				result = true;
			}
			return result;
		}

		public static int GetNewCrossFittingStatus(IList<Pipe> lstPipe)
		{
			if (lstPipe.Count != 4)
			{
				return -1;
			}
			int num = 0;
			IList<Pipe> list = MethodFixedPipeDiameter.SortListPipe(lstPipe);
            double num2 = list[0].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num3 = list[1].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num4 = list[2].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
            double num5 = list[3].GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble() * 304.8;
			List<int> list2 = new List<int>();
			List<int> list3 = new List<int>();
			MethodFixedPipeDiameter.GetListFour(out list2, out list3);
			double num6;
			double num7;
			if (num2 == num3 && num4 >= num5 && num2 > num4)
			{
				num6 = num4;
				num7 = num2;
			}
			else if (num2 == num3 && num4 <= num5 && num2 > num5)
			{
				num6 = num5;
				num7 = num2;
			}
			else if (num2 >= num3 && num4 == num5 && num4 > num2)
			{
				num6 = num2;
				num7 = num4;
			}
			else
			{
				if (num2 > num3 || num4 != num5 || num4 <= num3)
				{
					return -1;
				}
				num6 = num3;
				num7 = num4;
			}
			int num8 = 0;
			for (int i = 0; i < list2.Count; i++)
			{
				if ((double)list2[i] >= num7)
				{
					num8 = list3[i];
					break;
				}
			}
			if (num8 == 0)
			{
				num8 = list3.Last<int>();
			}
			if (num2 == num3 || num4 == num5)
			{
				num++;
				if (num2 > num4)
				{
					if (num2 < 65.0 || num2 > 200.0)
					{
						return -1;
					}
					num++;
					if ((double)num8 < num6 || num6 < 25.0)
					{
						return -1;
					}
					num++;
				}
				else
				{
					if (num4 < 65.0 || num4 > 200.0)
					{
						return -1;
					}
					num++;
					if (num6 > (double)num8 || num2 < 25.0)
					{
						return -1;
					}
					num++;
				}
				if (num == 3)
				{
					if (num2 < num4)
					{
						if (num4 == 65.0 || num4 == 80.0)
						{
							num++;
						}
						if (num6 >= 25.0 && num6 <= 40.0)
						{
							num++;
						}
					}
					else
					{
						if (num2 == 65.0 || num2 == 80.0)
						{
							num++;
						}
						if (num6 >= 25.0 && num6 <= 40.0)
						{
							num++;
						}
					}
				}
				return num;
			}
			return -1;
		}
	}
}
